package com.example.demo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.base.CommonPage;
import com.example.demo.base.YesNoEnum;
import com.example.demo.dto.UserPageParam;
import com.example.demo.dto.UserParam;
import com.example.demo.entity.User;
import com.example.demo.enums.AdminTypeEnum;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.util.LowCodeServiceUtil;
import com.example.demo.vo.UserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author vicente
 * @since 2024-01-23
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(UserParam param) {
        param.setId(null);
        LowCodeServiceUtil.checkUnique(baseMapper,"userName",param.getUserName(),null,"用户名已存在，请检查userName参数");
        LowCodeServiceUtil.checkUnique(baseMapper,"mobilePhone",param.getMobilePhone(),null,"手机号已存在，请检查mobilePhone参数");
        User user = new User();
        BeanUtils.copyProperties(param,user);
        user.setSalt(RandomUtil.randomString(8));
        String md5Password = SecureUtil.md5(param.getPassword()+user.getSalt());
        user.setPassword(md5Password);
        user.setAdminType(AdminTypeEnum.COMMON_ADMIN.getCode());
        boolean success = super.save(user);
        if(success) {
            param.setId(user.getId());
        }
        return success;
    }

    @Override
    public boolean update(UserParam param) {
        return false;
    }

    @Override
    public IPage userPage(UserParam param) {
        Page<User> userPage = new Page<>(1,3);
        IPage<User> iPage = baseMapper.selectPageVo(userPage,param.getIsLocked());
        System.out.println("总页数："+iPage.getPages());
        System.out.println("总记录数："+iPage.getTotal());
        return iPage;
    }

    @Override
    public CommonPage<UserVO> page(UserPageParam param) {
        IPage<UserVO> page = param.buildMpPage();
        QueryWrapper queryWrapper = param.buildQueryWrapper();
        // 超级管理员不可见
        queryWrapper.ne("t.admin_type",AdminTypeEnum.SUPER_ADMIN);
        queryWrapper.eq("t.is_deleted", YesNoEnum.NO);
        List<UserVO> list = baseMapper.selectCustom(page, queryWrapper);
        page.setRecords(list);
        return CommonPage.toPage(page);
    }

    /**
     * 通过用户名查询
     * @param userName
     * @return
     */
    @Override
    public User selectByUserName(String userName) {
        List<User> userList =  this.baseMapper.selectList(Wrappers.lambdaQuery(User.class).eq(User::getUserName,userName));
        return CollectionUtil.isEmpty(userList) ? null : userList.get(0);
    }
    /**
     * 通过手机号查询
     * @param mobilePhone
     * @return
     */
    @Override
    public User selectByMobilePhone(String mobilePhone) {
        List<User> userList =  this.baseMapper.selectList(Wrappers.lambdaQuery(User.class).eq(User::getMobilePhone,mobilePhone));
        return CollectionUtil.isEmpty(userList) ? null : userList.get(0);
    }

    @Override
    public UserVO findById(Long id) {
        return null;
    }


}
